package cn.nubia.admin.application.service;

import cn.nubia.admin.application.bean.Soft;
import cn.nubia.admin.application.bean.SoftRecommend;
import cn.nubia.admin.application.bean.SoftRecommendVo;
import cn.nubia.admin.application.query.SoftRecommendQuery;
import cn.nubia.admin.common.mybatis.bean.Paged;
import cn.nubia.admin.common.mybatis.bean.Query;
import cn.nubia.admin.common.mybatis.service.BaseServiceImpl;
import cn.nubia.admin.common.springmvc.WebContext;
import cn.nubia.admin.common.utils.BeanUtils;
import cn.nubia.admin.common.utils.DateUtil;
import cn.nubia.admin.sysconfig.bean.SystemLog;
import cn.nubia.admin.sysconfig.service.SystemLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by cnJasonon 2015/7/10.
 * 软件推荐service
 */
@Service
public class SoftRecommendService extends BaseServiceImpl<SoftRecommend> {


    @Autowired
    private SoftService softServive;
    @Autowired
    SystemLogService systemLogService;

    /**
     * 通过软件id来获得推荐对象
     *
     * @param softId 软件id
     * @return 推荐内容
     */
    public SoftRecommend getRecommendBySoftId(Integer softId) {
        Query<SoftRecommend> query = Query.build(SoftRecommend.class);
        query.addEq("softId", softId);
        return get(query);
    }

    /**
     * 保存或者修改软件推荐信息.
     *
     * @param softRecommend 软件推荐信息.
     */
    public void save(SoftRecommend softRecommend) {
        Integer curr = DateUtil.getCurrentTime();
        Integer adminId = WebContext.getCachedUser().getId();//WebContext.getCachedUser().getId()
        String  adminName = WebContext.getCachedUser().getName();//WebContext.getCachedUser().getName()
        SoftRecommend recommend = getRecommendBySoftId(softRecommend.getSoftId());
        if (null == recommend) {
            softRecommend.setCreateTime(curr);
            softRecommend.setCreateAdminId(adminId);
            softRecommend.setCreateAdminName(adminName);
            insert(softRecommend);
            systemLogService.log(SystemLog.ModuleTypeEnum.SOFTRECOMMENDLIST, SystemLog.OperateTypeEnum.ADD, softRecommend.getId());

        } else {
            softRecommend.setCreateAdminName(recommend.getCreateAdminName());
            softRecommend.setCreateAdminId(recommend.getCreateAdminId());
            softRecommend.setCreateTime(recommend.getCreateTime());
            softRecommend.setModifyAdminId(adminId);
            softRecommend.setModifyAdminName(adminName);
            update(softRecommend);
            systemLogService.log(SystemLog.ModuleTypeEnum.SOFTRECOMMENDLIST, SystemLog.OperateTypeEnum.EDIT, softRecommend.getId());

        }
    }

    public Paged<SoftRecommendVo> findPagedList(SoftRecommendQuery softRecommendQuery) {
        Query<SoftRecommend> query = Query.build(SoftRecommend.class);
        query.addEq("softId", softRecommendQuery.getSoftId());
        query.addEq("recType", softRecommendQuery.getRecType());
        query.setPaged(softRecommendQuery.getPageNo(), softRecommendQuery.getPageSize());

        Paged<SoftRecommend> paged = findPagedByQuery(query);

        Paged<SoftRecommendVo> voPaged = new Paged<SoftRecommendVo>();
        List<SoftRecommendVo> list = new ArrayList<SoftRecommendVo>();
        for (int i = 0; i < paged.getListData().size(); i++) {
            SoftRecommend recommend = paged.getListData().get(i);
            SoftRecommendVo vo = new SoftRecommendVo();
            BeanUtils.copyProperties(recommend,vo);
            if(vo.getRecType() != null){
                if(vo.getRecType() == 1){
                    vo.setRecName("精品推荐");
                }else if(vo.getRecType() ==2){
                    vo.setRecName("应用");
                }else if(vo.getRecType() ==3){
                    vo.setRecName("游戏");
                }
            }
            if(vo.getSoftId() != null){
                Soft soft = softServive.get(vo.getSoftId());
                if(soft != null){
                   vo.setSoftName(soft.getSoftName());
                }
            }
            list.add(vo);
        }

        voPaged.setListData(list);
        voPaged.setTotalCount(paged.getTotalCount());
        voPaged.setPageNo(paged.getPageNo());
        voPaged.setPageSize(paged.getPageSize());

        return voPaged;
    }

    public boolean deleteSoftRecommend(Integer id) {
        SoftRecommend recommend = get(id);
        delete(recommend);
        systemLogService.log(SystemLog.ModuleTypeEnum.SOFTRECOMMENDLIST, SystemLog.OperateTypeEnum.DEL, id);

        return true;
    }
}
